﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InventoryProject.Framework.Entity;
using InventoryProject.FrameWork;
using System.Data;
using FrameWork.FrameWork.Utility;

namespace InventoryProject.Setting
{
    public partial class FormManager : System.Web.UI.Page
    {
        FormEntity _entity;
        FormAccess _access;
        string LoginID;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginID = "admin"; // = Session["LogID"]
            //Insure that the __doPostBack() JavaScript method is created...
            this.ClientScript.GetPostBackEventReference(this, string.Empty);
            if (!IsPostBack)
            {

                LoadData("");
            }
            else
            {
                if ((Request.Form["__EventTarget"] != null && Request.Form["__EventTarget"] == "myDblClick"))

                {
                    grdData.SelectedIndex = int.Parse(Request.Form["__EVENTARGUMENT"].ToString());
                    GridViewRow dr;
                    dr = grdData.SelectedRow;
                    txtID.Value = grdData.DataKeys[grdData.SelectedIndex].Value.ToString();
                    txtFormID.Text = dr.Cells[1].Text;
                    txtFormNm.Text = HttpUtility.HtmlDecode(dr.Cells[2].Text);
                    txtFilePath.Text = dr.Cells[3].Text;
                    txtModuleID.Text = dr.Cells[4].Text;
                    
                }
            }
        }

        private void LoadData(string FormID)
        {
            _access = new FormAccess();
            DataTable dtData = _access.tForm_Qry(FormID);
            grdData.DataSource = dtData;
            grdData.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string SType = "";
            _access = new FormAccess();
            DataTable dtRes;
            _entity = new FormEntity();
            _entity.ID = (txtID.Value.Trim()=="") ? 0 : Int32.Parse(txtID.Value.Trim());
            _entity.FormID = txtFormID.Text;
            _entity.FormNm = txtFormNm.Text;
            _entity.FilePath = txtFilePath.Text;
            _entity.ModuleID = txtModuleID.Text;

            if (txtID.Value.Trim() == "")
                SType = "A";
            else
                SType = "U";

            dtRes = _access.tForm_Save(SType, _entity, LoginID);

            MessageHelper.ShowMessage(this, dtRes.Rows[0][1].ToString());
            txtID.Value = dtRes.Rows[0][2].ToString();

            LoadData("");
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            ClearData();
        }

        protected void grdData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
        }

        private void ClearData()
        {
            txtID.Value = "";
            txtFormID.Text = "";
            txtFormNm.Text = "";
            txtFilePath.Text = "";
            txtModuleID.Text = "";
        }

      

        protected void grdData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            _access = new FormAccess();
            _entity = new FormEntity();
            DataTable dtRes;
            _entity.ID = Int32.Parse(grdData.DataKeys[e.RowIndex].Value.ToString());
            _entity.FormID = e.Values[0].ToString();
            dtRes = _access.tForm_Save("D", _entity, LoginID);
            if (dtRes.Rows[0][0].ToString() == "OK")
            {
                MessageHelper.ShowMessage(this, dtRes.Rows[0][1].ToString());
                LoadData("");
                ClearData();
            }
            else
                MessageHelper.ShowMessage(this, dtRes.Rows[0][1].ToString());
        }

        protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';this.style.color='red'";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.color='black';";
                e.Row.Attributes.Add("onclick", "Javascript:__doPostBack('myDblClick','" + e.Row.RowIndex + "');");
            }
        }


    }
}